$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'mercharReport/storematerialindetail/list',
        datatype: "json",
        colModel: [			
			{ label: '单据号', name: 'applyNo', index: 'applyNo', width: 100, key: true ,align:'center'},
			{ label: '采购总金额', name: 'totalMoney', index: 'totalMoney', width: 80 ,align:'center'},
			{ label: '是否含税', name: 'tax', index: 'tax', width: 80 ,align:'center',
                formatter: function (value, options, row) {
                    return value == 1 ? '是' : '否';
                }},
			{ label: '申请门店名称', name: 'sellerName', index: 'sellerName', width: 80 ,align:'center'},
			{ label: '采购申请单状态', name: 'status', index: 'status', width: 80 ,align:'center',
                formatter: function (value, options, row) {
                    return value == 1 ? '待审核' : (value==2?'已审核':'已入库');
                }},
			{ label: '采购备注', name: 'applyRemark', index: 'applyRemark', width: 80 ,align:'center'},
			{ label: '采购类型', name: 'purchaseType', index: 'purchaseType', width: 80 ,align:'center',
                formatter: function (value, options, row) {
                    return value == 1 ? '纸张' : '普通耗材';
                }},
			{ label: '支付时间', name: 'financePaytime', index: 'financePaytime', width: 80 ,align:'center'},
			{ label: '付款备注', name: 'financeRemark', index: 'financeRemark', width: 80 ,align:'center'},
			{ label: '财务审核状态', name: 'financeStatus', index: 'financeStatus', width: 80 ,align:'center',
                formatter: function (value, options, row) {
                    return value == 1 ? '待付款' : (value==2?'已付款':'待审核');
                }},
			{ label: '采购申请单耗材状态', name: 'detailStatus', index: 'detailStatus', width: 80 ,align:'center',
                formatter: function (value, options, row) {
                    return value == 1 ? '待入库' : (value==2?'部分入库':'已完成');
                }},
			{ label: '耗材名称', name: 'materialName', index: 'materialName', width: 80 ,align:'center'},
			{ label: '耗材申请数量', name: 'materialQuantity', index: 'materialQuantity', width: 80 ,align:'center'},
			{ label: '耗材实际入库数量', name: 'receiveQuantity', index: 'receiveQuantity', width: 80 ,align:'center'},
			{ label: '采购供应商名称', name: 'supplierName', index: 'supplierName', width: 80 ,align:'center'},
			{ label: '耗材单价', name: 'unitMoney', index: 'unitMoney', width: 80 ,align:'center'},
			{ label: '耗材总价', name: 'detailTotalMoney', index: 'detailTotalMoney', width: 80 ,align:'center'},
			{ label: '耗材单位', name: 'unit', index: 'unit', width: 80 ,align:'center'}
        ],
		viewrecords: true,
        height: window.innerHeight-125,
        rowNum: 25,
        rowList: [25, 50, 80],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		storeMaterialInDetail: {},
		q:{
            applyNo:'',
            materialName:'',
            supplierName:'',
		}
	},
	methods: {
        checkBeginDate: function () {
            WdatePicker({
                dateFmt: 'yyyy-MM-dd HH:mm:ss', isShowClear: false, onpicking: function (dp) {
                    vm.q.beginCreatetime = dp.cal.getNewDateStr();
                }
            })
        },
        checkEndDate: function () {
            WdatePicker({
                dateFmt: 'yyyy-MM-dd HH:mm:ss', isShowClear: false, onpicking: function (dp) {
                    vm.q.endCreatetime = dp.cal.getNewDateStr();
                }
            })
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.storeMaterialInDetail = {};
		},
		update: function (event) {
			var detailId = getSelectedRow();
			if(detailId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(detailId)
		},
		saveOrUpdate: function (event) {
			var url = vm.storeMaterialInDetail.detailId == null ? "mercharReport/storematerialindetail/save" : "mercharReport/storematerialindetail/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.storeMaterialInDetail),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var detailIds = getSelectedRows();
			if(detailIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "mercharReport/storematerialindetail/delete",
                    contentType: "application/json",
				    data: JSON.stringify(detailIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(detailId){
			$.get(baseURL + "mercharReport/storematerialindetail/info/"+detailId, function(r){
                vm.storeMaterialInDetail = r.storeMaterialInDetail;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData: {//postData此数组内容直接赋值到url上，参数类型：{name1:value1…}
                    'applyNo': vm.q.applyNo.trim(),
                    'materialName': vm.q.materialName.trim(),
                    'supplierName': vm.q.supplierName.trim(),
                },
                page:1
            }).trigger("reloadGrid");
		},
        reset() {
            vm.q.applyNo = '';
            vm.q.materialName = '';
            vm.q.supplierName = '';
            vm.reload();
        },
        exportExcel() {
            //导出excel
            vm.q.applyNo = vm.q.applyNo.trim();
            vm.q.materialName = vm.q.materialName.trim();
            vm.q.supplierName = vm.q.supplierName.trim();
            const data = encodeURIComponent(JSON.stringify(vm.q));
            window.location.href = baseURL + "mercharReport/storematerialindetail/exportExcel?postData=" + data;
        }
	}
});